// components/blog-ctrl/blog-ctrl.js
let userInfo = {}
const db = wx.cloud.database()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    blogId: String,
    blog: Object,
  },
  externalClasses: ['iconfont', 'icon-pinglun', 'icon-fenxiang'],
  /**
   * 组件的初始数据
   */
  data: {
    // 登录组件是否显示
    loginShow: false,
    // 底部弹出层是否显示
    modalShow: false,
    content: '',
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onComment() {
      // 判断用户是否授权
      wx.getSetting({
        success: (res) => {
          if (res.authSetting['scope.userInfo']) {
            wx.getUserInfo({
              success: (res) => {
                userInfo = res.userInfo
                // 显示评论弹出层
                this.setData({
                  modalShow: true,
                })
              }
            })
          } else {
            this.setData({
              loginShow: true,
            })
          }
        }
      })
    },
    onLoginsuccess(event) {
      userInfo = event.detail
      // 授权框消失，评论框显示
      this.setData({
        loginShow: false,
      }, () => {
        this.setData({
          modalShow: true,
        })
      })
    },

    onLoginfail() {
      wx.showModal({
        title: '授权用户才能进行评价',
        content: '',
      })
    },
    onInput(event) {
      this.setData({
        content: event.detail.value
      })
    },
    onSend(event) {

      const templateId = 'ylFE4cuyk52n_UKt1otNX-FarynIEd6wwtiNP8v3MFU'
      wx.requestSubscribeMessage({
        tmplIds: [templateId],
        success(res) {
          if (res[templateId] == 'accept') {
            //用户同意了订阅，允许订阅消息
            wx.showToast({
              title: '订阅成功'
            })
          } else {
            //用户拒绝了订阅，禁用订阅消息
            wx.showToast({
              title: '订阅失败'
            })
          }
        },
        fail(err) {
          console.error(err)
        }
      })


      //console.log(event)
      //插入数据库
      // 插入数据库
      let content = this.data.content
      if (content.trim() == '') {
        wx.showModal({
          title: '评论内容不能为空',
          content: ''
        })
        return
      }

      //过滤敏感词

    wx.cloud.callFunction({
      name: "msgSC",
      data: {
        content
      }
    }).then(res=> {
      if (res.result.code == 200) {
        wx.showLoading({
          title: '评价中',
          mask: true
        })
   
        db.collection('blog-comment').add({
          data: {
            content,
            createTime: db.serverDate(),
            blogId: this.properties.blogId,
            nickName: userInfo.nickName,
            avatarUrl: userInfo.avatarUrl
          }
        }).then((res) => {
  
          // 推送模板消息
          wx.cloud.callFunction({
            name: 'sendMessage',
            data: {
              content,
              blogId: this.properties.blogId
            }
          }).then((res) => {
            console.log(res)
          })
  
  
          wx.hideLoading()
          wx.showToast({
            title: '评论成功',
          })
          this.setData({
            modalShow: false,
            content: ''
          })
        })
      } else {
        wx.hideLoading()
        wx.showModal({
          title: '发布的内容含有违法违规内容',
          content: '',
        })
      }
    })
 
      
 
    }
  }
})
